package mortal.game.control
{
    import Message.Game.*;
    import Message.Public.*;
    import modules.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene.*;
    import mortal.game.scene.ai.*;
    import mortal.game.scene.layer.utils.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.stall.defin.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class StallController extends Controller
    {
        private var _stallModule:StallModule;
        private var _isSelf:Boolean = false;
        private var _isInStall:Boolean = false;

        public function StallController()
        {
            
        }

        override protected function initView() : IView
        {
            if (!this._stallModule)
            {
                this._stallModule = new StallModule();
                this._stallModule.x = 400;
                this._stallModule.y = 100;
                this._stallModule.addEventListener(WindowEvent.CLOSE, this.hideStallHandler);
            }
            return this._stallModule;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.StallReady, this.readyHandler);
            Dispatcher.addEventListener(EventName.StallEndStall, this.endStallHandler);
            Dispatcher.addEventListener(EventName.StallOpenStall, this.openStallHandler);
            Dispatcher.addEventListener(EventName.StallCloseStall, this.closeStallHandler);
            NetDispatcher.addCmdListener(ServerCommand.StallOpenSuccess, this.openSuccessHandler);
            
        }

        private function resetListenerState() : void
        {
            this.hideStallHandler();
            this.addStallListeners();
            
        }

        private function addStallListeners() : void
        {
            this.updateMoney();
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            Dispatcher.addEventListener(EventName.StallAddMsg, this.addMsgHandler);
            Dispatcher.addEventListener(EventName.StallRequestStart, this.startStallHandler);
            Dispatcher.addEventListener(EventName.StallFastOp, this.stallFashOpHandler);
            if (this.isSelf)
            {
                Dispatcher.addEventListener(EventName.StallAddItem, this.addItemHandler);
                Dispatcher.addEventListener(EventName.StallDownItem, this.downItemHandler);
                Dispatcher.addEventListener(EventName.StallChangeName, this.changeNameHandler);
                Dispatcher.addEventListener(EventName.StallChangeBoothMsg, this.stallChangeBoothMsg);
                Dispatcher.addEventListener(EventName.StallClearMsg, this.clearMsgHandler);
                NetDispatcher.addCmdListener(ServerCommand.StallItemsUpdate, this.updateItems);
                NetDispatcher.addCmdListener(ServerCommand.StallMyStallNameUpdate, this.onUpdateMyStallName);
                NetDispatcher.addCmdListener(ServerCommand.StallMyIntroduceUpdate, this.onUpdateMyIntroduce);
                NetDispatcher.addCmdListener(ServerCommand.StallSelfLeaveWordsUpdate, this.onUpdateSelfLeaveWords);
                NetDispatcher.addCmdListener(ServerCommand.StallSelfLeaveWordsClear, this.onClearSelfLeaveWords);
            }
            else
            {
                Dispatcher.addEventListener(EventName.StallBuyItem, this.onBuyItem);
                Dispatcher.addEventListener(EventName.StallUserCloseStall, this.onUserCloseStall);
                NetDispatcher.addCmdListener(ServerCommand.StallOtherItemsUpdate, this.updateOtherItems);
                NetDispatcher.addCmdListener(ServerCommand.StallOtherLeaveWordsUpdate, this.onUpdateOtherLeaveWords);
                NetDispatcher.addCmdListener(ServerCommand.StallOtherLeaveWordsClear, this.onClearOtherLeaveWords);
            }
            
        }

        private function hideStallHandler(event:WindowEvent = null) : void
        {
            Dispatcher.removeEventListener(EventName.StallRequestStart, this.startStallHandler);
            Dispatcher.removeEventListener(EventName.StallAddMsg, this.addMsgHandler);
            Dispatcher.removeEventListener(EventName.StallAddItem, this.addItemHandler);
            Dispatcher.removeEventListener(EventName.StallDownItem, this.downItemHandler);
            Dispatcher.removeEventListener(EventName.StallChangeName, this.changeNameHandler);
            Dispatcher.removeEventListener(EventName.StallFastOp, this.stallFashOpHandler);
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            NetDispatcher.removeCmdListener(ServerCommand.StallItemsUpdate, this.updateItems);
            NetDispatcher.removeCmdListener(ServerCommand.StallMyStallNameUpdate, this.onUpdateMyStallName);
            Dispatcher.removeEventListener(EventName.StallBuyItem, this.onBuyItem);
            Dispatcher.removeEventListener(EventName.StallUserCloseStall, this.onUserCloseStall);
            NetDispatcher.removeCmdListener(ServerCommand.StallOtherItemsUpdate, this.updateOtherItems);
            NetDispatcher.removeCmdListener(ServerCommand.StallSelfLeaveWordsUpdate, this.onUpdateSelfLeaveWords);
            NetDispatcher.removeCmdListener(ServerCommand.StallSelfLeaveWordsClear, this.onClearSelfLeaveWords);
            NetDispatcher.removeCmdListener(ServerCommand.StallOtherLeaveWordsUpdate, this.onUpdateOtherLeaveWords);
            NetDispatcher.removeCmdListener(ServerCommand.StallOtherLeaveWordsClear, this.onClearOtherLeaveWords);
            NetDispatcher.removeCmdListener(ServerCommand.StallMyIntroduceUpdate, this.onUpdateMyIntroduce);
            Dispatcher.removeEventListener(EventName.StallAddMsg, this.addMsgHandler);
            Dispatcher.removeEventListener(EventName.StallClearMsg, this.clearMsgHandler);
            
        }

        private function readyHandler(event:DataEvent) : void
        {
            if (!view.isHide && this.isSelf)
            {
                
            }
            if (this._isInStall)
            {
                this.isSelf = true;
                view.show();
                this.isInStall = true;
                this.resetListenerState();
                this.resetSelfStallName();
                this.updateList();
                this.updateSelfStallBoothMessage();
                
            }
            this.isSelf = true;
            view.show();
            this.isInStall = false;
            this.resetListenerState();
            this.resetSelfStallName();
            NetDispatcher.addCmdListener(ServerCommand.StallStartStallResult, this.onStartResult);
            this.updateList();
            
        }

        private function openStallHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SEntityId;
            if (EntityUtil.toString(_loc_2) == EntityUtil.toString(cache.role.entityInfo.entityId))
            {
                if (this._isInStall)
                {
                    view.show();
                    this.isSelf = true;
                    this.isInStall = true;
                    this.resetListenerState();
                    this.resetSelfStallName();
                    this.updateList();
                    this.updateSelfStallBoothMessage();
                    
                }
            }
            else
            {
                GameProxy.stallProxy.openStall(_loc_2);
            }
            
        }

        private function openSuccessHandler(event:DataEvent) : void
        {
            this.isSelf = false;
            (view as StallModule).showOther();
            this.resetListenerState();
            this.clearStallView();
            
        }

        private function closeStallHandler(event:DataEvent) : void
        {
            GameProxy.stallProxy.closeStall();
            view.hide();
            
        }

        private function clearStallView() : void
        {
            
        }

        private function resetSelfStallName() : void
        {
            if (cache.pack.stallPackCache.stallName)
            {
                this.udpateStallName(cache.pack.stallPackCache.stallName, true);
            }
            else
            {
                GameProxy.stallProxy.myStallName();
            }
            
        }

        private function onUpdateMyStallName(param1:String) : void
        {
            this.udpateStallName(param1, true);
            
        }

        private function onUpdateMyIntroduce(param1:String) : void
        {
            this.updateStallIntroduce(param1, true);
            
        }

        private function onStartResult(param1:Boolean) : void
        {
            this.isInStall = param1;
            
        }

        private function startStallHandler(event:DataEvent) : void
        {
            var e:* = event;
            AIManager.cancelAll();
            if (RolePlayer.instance.isTurned)
            {
                MsgManager.addBroadCast("幻化状态不能摆摊！");
                
            }
            if (RolePlayer.instance.isCanStall() == false)
            {
                MsgManager.addBroadCast("离其他玩家的摊位太近，请再找个地方！");
                
            }
            if (RolePlayer.instance.isDoubleRest)
            {
                ScreenOperation.interruptDoubleRest(function () : void
            {
                startStall();
                
            }
            );
                
            }
            if (Cache.instance.fish.isInFishing)
            {
                ScreenOperation.interruptFish(function () : void
            {
                startStall();
                
            }
            );
                
            }
            if (Cache.instance.fish.isInRoastFish)
            {
                ScreenOperation.interruptRoastFish(function () : void
            {
                startStall();
                
            }
            );
                
            }
            this.startStall();
            
        }

        private function startStall() : void
        {
            this.isSelf = true;
            view.show();
            this.resetListenerState();
            GameProxy.stallProxy.startStall();
            
        }

        private function endStallHandler(event:DataEvent = null) : void
        {
            view.hide();
            GameProxy.stallProxy.endStall();
            this.isInStall = false;
            
        }

        private function updateList(param1:Boolean = false) : void
        {
            (view as StallModule).refreshItemList(cache.pack.stallPackCache.getStallItemsAtPage(1), true);
            if (param1 && cache.pack.stallPackCache.validatelength == 0)
            {
                this.endStallHandler();
            }
            
        }

        private function addItemHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SellItemData;
            _loc_2.fromIndex = cache.pack.backPackCache.getIndexByUid(_loc_2.uid);
            GameProxy.stallProxy.upItem(_loc_2);
            
        }

        private function downItemHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            if (_loc_2)
            {
                GameProxy.stallProxy.downItem(_loc_2.uid);
            }
            else
            {
                MsgManager.addBroadCast("请先选择需要下架的物品");
            }
            
        }

        private function stallFashOpHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            if (this.isSelf)
            {
                if (_loc_2)
                {
                    GameProxy.stallProxy.downItem(_loc_2.uid);
                }
                else
                {
                    MsgManager.addBroadCast("请先选择需要下架的物品");
                }
            }
            else
            {
                (view as StallModule).showBuyItemWindow(_loc_2);
            }
            
        }

        private function updateSelfStallBoothMessage() : void
        {
            this.updateStallIntroduce(cache.pack.stallPackCache.stallIntroduce, true);
            (view as StallModule).clearLeaveMsg(true);
            this.updateLeaveWords(cache.pack.stallPackCache.leaveWords, true);
            
        }

        private function changeNameHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            GameProxy.stallProxy.modifyStallName(_loc_2);
            
        }

        private function addMsgHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            if (this.isSelf)
            {
                GameProxy.stallProxy.addLeaveMsg(_loc_2, cache.role.entityInfo.entityId);
            }
            else
            {
                GameProxy.stallProxy.addLeaveMsg(_loc_2, cache.stallOtherCache.entityId);
            }
            
        }

        private function stallChangeBoothMsg(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            GameProxy.stallProxy.modifyStallIntroduce(_loc_2);
            
        }

        private function clearMsgHandler(event:DataEvent) : void
        {
            GameProxy.stallProxy.clearLeaveMsg();
            
        }

        private function openStall(param1:SEntityId, param2:String) : void
        {
            this.udpateStallName(param2, false);
            this.isSelf = false;
            GameProxy.stallProxy.openStall(param1);
            
        }

        private function udpateStallName(param1:String, param2:Boolean) : void
        {
            (view as StallModule).setStallName(param1, param2);
            
        }

        private function updateStallIntroduce(param1:String, param2:Boolean) : void
        {
            (view as StallModule).setStallIntroduce(param1, param2);
            
        }

        private function updateMoney(param1:Object = null) : void
        {
            var _loc_2:* = cache.login.loginGame.money;
            (view as StallModule).updateMoney(_loc_2.coin, _loc_2.gold);
            
        }

        private function updateItems(param1:Object) : void
        {
            if (param1)
            {
                this.updateList(true);
            }
            else
            {
                this.updateList(false);
            }
            
        }

        private function onUpdateSelfLeaveWords(param1:Array) : void
        {
            this.updateLeaveWords(param1, true);
            
        }

        private function onUpdateOtherLeaveWords(param1:Array) : void
        {
            this.updateLeaveWords(param1, false);
            
        }

        private function updateLeaveWords(param1:Array, param2:Boolean = true) : void
        {
            var _loc_3:SStallLeaveWord = null;
            for each (_loc_3 in param1)
            {
                
                if (_loc_3.type == 0)
                {
                    (view as StallModule).addBuyMsg(_loc_3.leaveWordDt, _loc_3.player, _loc_3.gold, _loc_3.coin, _loc_3.amount, _loc_3.playerItem, param2);
                    continue;
                }
                if (_loc_3.type == 1)
                {
                    (view as StallModule).addLeaveWord(_loc_3.leaveWordDt, _loc_3.player, _loc_3.msg, param2);
                }
            }
            
        }

        private function onBuyItem(event:DataEvent) : void
        {
            var _loc_2:* = event.data as BuyItemData;
            GameProxy.stallProxy.buyItem(_loc_2.uid, _loc_2.buyNum, _loc_2.coin, _loc_2.gold);
            
        }

        private function onClearOtherLeaveWords(param1:Object) : void
        {
            (view as StallModule).clearLeaveMsg(false);
            
        }

        private function onClearSelfLeaveWords(param1:Object) : void
        {
            (view as StallModule).clearLeaveMsg(true);
            
        }

        private function updateOtherItems(param1:Object) : void
        {
            (view as StallModule).setStallName(cache.stallOtherCache.stallName, false);
            (view as StallModule).setStallIntroduce(cache.stallOtherCache.stallIntroduce, false);
            (view as StallModule).refreshItemList(cache.stallOtherCache.getStallItemsAtPage(1), false);
            (view as StallModule).updatePlayerMiniInfo(cache.stallOtherCache.miniInfo);
            
        }

        private function onUserCloseStall(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SEntityId;
            if (!view.isHide && !this._isSelf && EntityUtil.toString(_loc_2) == EntityUtil.toString(cache.stallOtherCache.entityId))
            {
                view.hide();
                MsgManager.addBroadCast("该玩家已经收摊");
            }
            
        }

        public function set isSelf(param1:Boolean) : void
        {
            this._isSelf = param1;
            
        }

        public function get isSelf() : Boolean
        {
            return this._isSelf;
        }

        public function set isInStall(param1:Boolean) : void
        {
            this._isInStall = param1;
            (view as StallModule).isInStall = param1;
            
        }

        public function get isInStall() : Boolean
        {
            return this._isInStall;
        }

    }
}
